/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(function () {
    $("#ApplyDate").blur(function () {
        gethuankuanriqi();
    });
    $("#ApplyAmount").blur(function () {
        var applyamount = $("#ApplyAmount").val();
        var dayInterest = $("#dayInterest").val();
        var serviceCharge = $("#serviceCharge").val();
        var days = $("#ApplyDateLength").val();
        var amount = accAdd(accAdd(applyamount, applyamount * dayInterest * days / 100), applyamount * serviceCharge / 100);
        $("#ReimAmount").val(amount);
    });
    $("#ApplyDateLength").blur(function () {
        gethuankuanriqi();
        var applyamount = $("#ApplyAmount").val();
        var dayInterest = $("#dayInterest").val();
        var serviceCharge = $("#serviceCharge").val();
        var days = $("#ApplyDateLength").val();
        var amount = accAdd(accAdd(applyamount, applyamount * dayInterest * days / 100), applyamount * serviceCharge / 100);
        $("#ReimAmount").val(amount);
    });
    var name2Id = {}; //姓名对应的id
    $.fn.typeahead.Constructor.prototype.blur = function () {
        var that = this;
        setTimeout(function () {
            that.hide()
        }, 250);
    };
    $("#UserName").typeahead({
        source: function (query, process) {
//return ["红蒜小混级","红蒜大混级","一般红蒜","白蒜小混级","白蒜大混级","一般白蒜"];
            $.post('getUserByName', {text: query}, function (data) {
                var results = $.map(data, function (item) {
                    name2Id[item.user_nickname] = item.id; //键值对保存下来。
                    return item.user_nickname;
                });
                process(results);
            }, 'json');
        },
        updater: function (id) {
//            var results = $.map(dataresult, function (item) {
//                return item.cropname;
//            });
            console.log("updater:" + name2Id[id]);
            $("#LogisticsId").val(name2Id[id]);
            return id;
        }, afterSelect: function (item) {
            console.log("afterSelect:" + name2Id[id]); //打印对应的id
            // $("#UserId").val(name2Id[id]);
        },
    });
    $("#CustomerName").typeahead({
        source: function (query, process) {

            $.post('getCustomerByName', {text: query}, function (data) {
                var results = $.map(data, function (item) {
                    name2Id[item.user_nickname] = item.id; //键值对保存下来。
                    return item.user_nickname;
                });
                process(results);
            }, 'json');
        },
        updater: function (id) {
//            var results = $.map(dataresult, function (item) {
//                return item.cropname;
//            });
            console.log("updater:" + name2Id[id]);
            $("#CustomerId").val(name2Id[id]);
            return id;
        }, afterSelect: function (item) {
            console.log("afterSelect:" + name2Id[id]); //打印对应的id
            // $("#UserId").val(name2Id[id]);
        },
    });
})
/**  
 ** 加法函数，用来得到精确的加法结果  
 ** 说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。  
 ** 调用：accAdd(arg1,arg2)  
 ** 返回值：arg1加上arg2的精确结果  
 **/
function accAdd(arg1, arg2) {
    var r1, r2, m, c;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2));
    if (c > 0) {
        var cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", "")) * cm;
        } else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm;
            arg2 = Number(arg2.toString().replace(".", ""));
        }
    } else {
        arg1 = Number(arg1.toString().replace(".", ""));
        arg2 = Number(arg2.toString().replace(".", ""));
    }
    return (arg1 + arg2) / m;
}

//给Number类型增加一个add方法，调用起来更加方便。  
Number.prototype.add = function (arg) {
    return accAdd(arg, this);
};
function gethuankuanriqi() {
    var start_day = $("#ApplyDate").val();
    var days = $("#ApplyDateLength").val();
    var d1 = new Date(start_day);
    var d2 = new Date(d1);
    // d2.setFullYear(d2.getFullYear()+1);      //当前时间+1年
    d2.setDate(d2.getDate() + parseInt(days)); //当前时间+7天
    console.log(d2);
    d2 = FormatDate(d2);
    console.log(d2);
    $("#RepayMentDateTime").val(d2);
}
function FormatDate(strTime) {
    var date = new Date(strTime);
    var formatedMonth = ("0" + (date.getMonth() + 1)).slice(-2);
    var formatedDate = ("0" + (date.getDate())).slice(-2);
    return date.getFullYear() + "-" + formatedMonth + "-" + formatedDate;
}